package com.hex.liyue.commonmodule.manager;

import com.hex.liyue.LiYueApplication;
import com.hex.liyue.commonmodule.config.Constant;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.SPUtil;
import com.hex.liyue.usermodule.bean.AddressResultBean;
import com.hex.liyue.usermodule.bean.LoginResultBean;
import com.hex.liyue.usermodule.bean.MyMessageResultBean;

import java.util.Map;

/**
 * Created by yukuo on 2016/4/19.
 * 这是一个用户管理类
 */
public class UserManager {
    /**
     * 登入时候保存用户信息
     *
     * @param loginResultBean
     */
    public static void saveUserInfo(LoginResultBean loginResultBean) {
        //用户ID
        SPUtil.putString(LiYueApplication.getInstance(), Constant.USER_ID, loginResultBean.data.uid);
        //用户头像
        SPUtil.putString(LiYueApplication.getInstance(), Constant.AVATOR, loginResultBean.data.avatar);
        //用户手机号
        SPUtil.putString(LiYueApplication.getInstance(), Constant.USER_PHONE, loginResultBean.data.mobile);
        //用户昵称
        SPUtil.putString(LiYueApplication.getInstance(), Constant.NICKNAME, loginResultBean.data.nickname);
        //用户自己的邀请码
        SPUtil.putString(LiYueApplication.getInstance(), Constant.SELF_INVITE_CODE, loginResultBean.data.self_invite_code);
        //用户上线的邀请码
        SPUtil.putString(LiYueApplication.getInstance(), Constant.INVITE_CODE, loginResultBean.data.invite_code);
        //用户余额
        SPUtil.putlong(LiYueApplication.getInstance(), Constant.BALANCE, loginResultBean.data.balance);
        //用户余额
        SPUtil.putString(LiYueApplication.getInstance(), Constant.USERTYPE, loginResultBean.data.user_type);

        if (loginResultBean.data.receive_address != null) {
            //用户默认地址
            SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_ID, loginResultBean.data.receive_address.delivery_id);
            //收货人
            SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_USERNAME, loginResultBean.data.receive_address.username);
            //收货人联系方式
            SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_MOBILE, loginResultBean.data.receive_address.mobile);
            //省市名
            SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_PROVINCE_NAME, loginResultBean.data.receive_address.province_name);
            //城市名
            SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_CITY_NAME, loginResultBean.data.receive_address.city_name);
            //街道名
            SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_DISTRICT_NAME, loginResultBean.data.receive_address.district_name);
            //详细地址
            SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_ADDRESS_DETAIL, loginResultBean.data.receive_address.address_detail);
        }
    }

    /**
     * 更新本地信息
     *
     * @param myMessageResultBean
     */
    public static void saveUserInfo(MyMessageResultBean myMessageResultBean) {
        //用户ID
        SPUtil.putString(LiYueApplication.getInstance(), Constant.USER_ID, myMessageResultBean.data.uid);
        //用户头像
        SPUtil.putString(LiYueApplication.getInstance(), Constant.AVATOR, myMessageResultBean.data.avatar);
        //用户手机号
        SPUtil.putString(LiYueApplication.getInstance(), Constant.USER_PHONE, myMessageResultBean.data.mobile);
        //用户昵称
        SPUtil.putString(LiYueApplication.getInstance(), Constant.NICKNAME, myMessageResultBean.data.nickname);
        //用户自己的邀请码
        SPUtil.putString(LiYueApplication.getInstance(), Constant.SELF_INVITE_CODE, myMessageResultBean.data.self_invite_code);
        //用户上线的邀请码
        SPUtil.putString(LiYueApplication.getInstance(), Constant.INVITE_CODE, myMessageResultBean.data.invite_code);
        //用户余额
        SPUtil.putlong(LiYueApplication.getInstance(), Constant.BALANCE, myMessageResultBean.data.balance);
    }

    /**
     * 更新收货地址
     *
     * @param dataBean
     */
    public static void updateAddress(AddressResultBean.DataBean dataBean) {
        //用户默认地址
        SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_ID, dataBean.delivery_id);
        //收货人
        SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_USERNAME, dataBean.username);
        //收货人联系方式
        SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_MOBILE, dataBean.mobile);
        //省市名
        SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_PROVINCE_NAME, dataBean.province_name);
        //城市名
        SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_CITY_NAME, dataBean.city_id);
        //街道名
        SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_DISTRICT_NAME, dataBean.district_name);
        //详细地址
        SPUtil.putString(LiYueApplication.getInstance(), Constant.DELIVERY_ADDRESS_DETAIL, dataBean.address_detail);
        //更新了打印一下
        showAll();
    }

    /**
     * 清除所有数据
     */
    public static void clearAllInfo() {
        SPUtil.clear(LiYueApplication.getInstance());
    }

    /**
     * 获取用户的头像
     *
     * @return 返回的头像
     */
    public static String getUserAvator() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.AVATOR, "");
    }

    /**
     * 获取用户的手机号
     *
     * @return 返回的手机号
     */
    public static String getUserPhone() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.USER_PHONE, "");
    }

    /**
     * 获取登录状态
     */
    public static boolean isLogin() {
        return SPUtil.getBoolean(LiYueApplication.getInstance(), Constant.ISLOGIN, false);
    }

    /**
     * 设置登录状态
     */
    public static void setIsLogin(boolean islogin) {
        SPUtil.putBoolean(LiYueApplication.getInstance(), Constant.ISLOGIN, islogin);
    }

    /**
     * 获取用户ID
     */
    public static String getUserId() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.USER_ID, "");
    }

    /**
     * 设置用户ID 环信用户名
     */
    public static void setUserId(String userId) {
        SPUtil.putString(LiYueApplication.getInstance(), Constant.USER_ID, userId);
    }

    /**
     * 获取自己的邀请码
     *
     * @return
     */
    public static String getSelfInviteCode() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.SELF_INVITE_CODE, "");
    }

    /**
     * 获取上线的邀请码
     *
     * @return
     */
    public static String getInviteCode() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.INVITE_CODE, "");
    }

    /**
     * 获取用户名
     *
     * @return
     */
    public static String getNickName() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.NICKNAME, "");
    }

    /**
     * 获取默认收货地址ID
     *
     * @return
     */
    public static String getDeliveryId() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_ID, "");
    }

    /**
     * 获取省市名
     *
     * @return
     */
    public static String getDeliveryProvinceName() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_PROVINCE_NAME, "");
    }

    /**
     * 获取城市名
     *
     * @return
     */
    public static String getDeliveryCityName() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_CITY_NAME, "");
    }

    /**
     * 获取街道名
     *
     * @return
     */
    public static String getDeliveryDistrictName() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_DISTRICT_NAME, "");
    }

    /**
     * 获取收货人联系方式
     *
     * @return
     */
    public static String getDeliveryMobile() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_MOBILE, "");
    }

    /**
     * 获取收货人
     *
     * @return
     */
    public static String getDeliveryUsername() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_USERNAME, "");
    }

    /**
     * 获取地址详情
     *
     * @return
     */
    public static String getDeliveryAddressDetail() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_ADDRESS_DETAIL, "");
    }

    /**
     * 更新省市名
     *
     * @return
     */
    public static void setDeliveryProvinceName() {
        SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_PROVINCE_NAME, "");
    }

    /**
     * 更新城市名
     *
     * @return
     */
    public static void setDeliveryCityName() {
        SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_CITY_NAME, "");
    }

    /**
     * 更新街道名
     *
     * @return
     */
    public static void setDeliveryDistrictName() {
        SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_DISTRICT_NAME, "");
    }

    /**
     * 更新收货人联系方式
     *
     * @return
     */
    public static void setDeliveryMobile() {
        SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_MOBILE, "");
    }

    /**
     * 更新收货人
     *
     * @return
     */
    public static void setDeliveryUsername() {
        SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_USERNAME, "");
    }

    /**
     * 更新地址详情
     *
     * @return
     */
    public static void setDeliveryAddressDetail() {
        SPUtil.getString(LiYueApplication.getInstance(), Constant.DELIVERY_ADDRESS_DETAIL, "");
    }


    /**
     * 更新上线邀请码
     */
    public static void setInviteCode(String inviteCode) {
        SPUtil.putString(LiYueApplication.getInstance(), Constant.INVITE_CODE, inviteCode);
    }

    /**
     * 更新余额
     */
    public static void setBalance(long balance) {
        SPUtil.putlong(LiYueApplication.getInstance(), Constant.BALANCE, balance);
    }

    /**
     * 获取余额
     */
    public static long getBalance() {
        return SPUtil.getLong(LiYueApplication.getInstance(), Constant.BALANCE, 0);
    }

    /**
     * 获取用户类型
     */
    public static String getUserType() {
        return SPUtil.getString(LiYueApplication.getInstance(), Constant.USERTYPE, "");
    }

    /**
     * 打印所有SP信息
     */
    public static void showAll() {
        Map<String, ?> map = SPUtil.getAll(LiYueApplication.getInstance());
        for (String key : map.keySet()) {
            L.d("WJ", key + ":" + map.get(key));
        }
    }

    /**
     * 这是一个设置是否是第一次打开app界面
     *
     * @param isFirst
     */
    public static void setIsFirst(boolean isFirst) {
        SPUtil.putBoolean(LiYueApplication.getInstance(), Constant.ISFIRST, isFirst);
    }

    /**
     * 这是一个获取是否是第一次打开app界面
     */
    public static boolean isFirst() {
        return SPUtil.getBoolean(LiYueApplication.getInstance(), Constant.ISFIRST, false);
    }

}
